const lodash = require("lodash");
const fs = require("fs-extra");
const path = require("path");
const arrayMode = require("ml-array-mode");
const DesertData = require("./Savage_Main_Squad_KillGroggyBluezone_II.json");
const MAP_SIZE = 408000;//CM
const GRID_LENGTH = 5100;//CM
const SMALL_RADIUS = 20000;//cm
const MatchesCount = 336;
const GRIDS_IN_NORMAL = (_ => {
  let length = MAP_SIZE / GRID_LENGTH;
  let arr = new Array(length);
  for (let i = 0; i < length; i++) {
    arr[i] = (new Array(length)).fill(0);
  }
  return arr;
})();
const GRIDS_IN_SMALL = (_ => {
  let length = MAP_SIZE / GRID_LENGTH;
  let arr = new Array(length);
  for (let i = 0; i < length; i++) {
    arr[i] = (new Array(length)).fill(0);
  }
  return arr;
})();

for (let zone of DesertData.SAFETY_ZONES) {
  for (let xi = 0; xi < GRIDS_IN_NORMAL.length; xi++) {
    for (let yi = 0; yi < GRIDS_IN_NORMAL[xi].length; yi++) {
      let [rectCenterX, rectCenterY] = [xi * GRID_LENGTH + GRID_LENGTH / 2, yi * GRID_LENGTH + GRID_LENGTH / 2];
      let distance2 = Math.pow(rectCenterX - zone.safetyZonePosition.x, 2) + Math.pow(rectCenterY - zone.safetyZonePosition.y, 2)
      if (distance2 <= Math.pow(zone.safetyZoneRadius, 2)) {
        GRIDS_IN_NORMAL[xi][yi] += 1;
        if (zone.safetyZoneRadius <= SMALL_RADIUS) {
          GRIDS_IN_SMALL[xi][yi] += 1
        }
        // debugger
      }
    }
  }
}
// for (let i = 0; i < GRIDS_IN_NORMAL.length; i++) {
//   for (let j = 0; j < GRIDS_IN_NORMAL[i].length; j++) {
//     GRIDS_IN_NORMAL[i][j] = GRIDS_IN_NORMAL[i][j] / MatchesCount;
//     GRIDS_IN_SMALL[i][j] = GRIDS_IN_SMALL[i][j] / MatchesCount;
//     // debugger
//   }
// }
let max_in_normal = lodash.max(lodash.flatten(GRIDS_IN_NORMAL));
let max_in_small = lodash.max(lodash.flatten(GRIDS_IN_SMALL));
fs.writeFile(path.join(__dirname, `SavageMap_SafetyZone_${SMALL_RADIUS}.json`), JSON.stringify({
  SMALL_RADIUS: SMALL_RADIUS,
  max_in_normal,
  max_in_small,
  GRIDS_IN_NORMAL,
  GRIDS_IN_SMALL,
  GRID_LENGTH
}), (err) => {
  debugger
});
debugger